package com.naiterui.ehp.bs.doctor.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.utils.encypt.RSAUtil;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;
import com.naiterui.ehp.bs.doctor.service.IDoctorService;
import com.naiterui.ehp.bs.doctor.vo.DoctorAuthVO;
import com.naiterui.ehp.bs.doctor.vo.DoctorMineVO;
import com.naiterui.ehp.bs.doctor.vo.DoctorParaRequestVO;
import com.naiterui.ehp.bs.doctor.vo.DoctorVO;
import com.naiterui.ehp.bs.doctor.vo.UserCenterVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;

@Validated
@Controller
@RequestMapping("ad/user")
@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.USER_INFO})
public class AdDoctorController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdDoctorController.class);

    @Autowired
    private IDoctorService doctorService;

    /**
     * 个人中心
     * DoctorController.center()
     *
     * @param doctorId 医生ID
     *
     * @return
     *
     * @Author guoyongxiang
     * @Date 2016年1月13日
     * @since 1.0.0
     */
    @ResponseBody
    @RequestMapping("center")
    @ApiOperation(value = "医生个人中心", notes = "根据医生ID获取详细信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "long", paramType = "query")})
    public ResponseVO<UserCenterVO> center(@NotNull(message = "医生ID不能为null") Long doctorId) throws BusinessException {

        LOGGER.info("个人中心 请求参数：doctorId = [{}]", doctorId);

        UserCenterVO vo = this.doctorService.getUserCenter(doctorId);

        return PublicService.returnResponseVO(vo);

    }

    /**
     * <根据医生ID获取详细信息>
     *
     * @param doctorId 医生id
     *
     * @return String <返回值描述>
     *
     * @Throws 异常信息
     * @History 2015-6-21 上午10:08:01 by guoyongxiang
     */
    @ResponseBody
    @RequestMapping("detail")
    @ApiOperation(value = "医生详情", notes = "根据医生ID获取详细信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "long", paramType = "query")})
    public ResponseVO<DoctorVO> detail(Long doctorId) throws BusinessException {

        LOGGER.info("根据医生ID获取详细信息 请求参数：doctorId = [{}]", doctorId);

        DoctorVO vo = this.doctorService.getDoctor(doctorId);

        return PublicService.returnResponseVO(vo);

    }

    /**
     * 修改医生信息(不包含医生资质上传)
     *
     * @param doctorParaRequestVO
     * @param verifyCode
     *
     * @return String <返回值描述>
     *
     * @throws BusinessException
     * @History 2015-6-21 上午10:16:37 by guoyongxiang
     */
    @ResponseBody
    @RequestMapping("save")
    @ApiOperation(value = "修改医生信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "verifyCode", value = "验证码", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "delType", value = "删除参数类型 1擅长2个人简介", required = true, dataType = "long", paramType = "query")})
    public ResponseVO<Void> save(DoctorParaRequestVO doctorParaRequestVO, String verifyCode, String password,
                                 @RequestParam(required = false) Integer delType) throws BusinessException {

        LOGGER.info("修改医生信息 请求参数：doctorParaRequestVO = [{}], verifyCode = [{}], password = [{}], delType = [{}]", doctorParaRequestVO, verifyCode,
                    password, delType);

        Long doctorId = doctorParaRequestVO.getDoctorId();

        if (doctorId == null) {
            LOGGER.error("医生ID为空");
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        if (password != null) {
            try {
                password = password.replaceAll(" ", "+");
                password = RSAUtil.decryptByPrivateKey(password);
            } catch (Exception e) {
                LOGGER.error("修改医生信息解密失败,doctorId={}", doctorId, e);
                throw new BusinessException(ExceptionCodes.FAILED, e);
            }
            password = password.trim();
        }

        this.doctorService.saveDoctor(doctorParaRequestVO, verifyCode, password, delType);

        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 个人资料提交(资质信息必传)
     * DoctorController.saveBase()
     *
     * @param doctorParaRequestVO 医生对象参数
     *
     * @return
     *
     * @Date 2016年2月27日
     * @since 2.2.0
     */
    @ResponseBody
    @RequestMapping("saveBase")
    @ApiOperation(value = "个人资料提交", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseVO<Void> saveBase(DoctorParaRequestVO doctorParaRequestVO) throws BusinessException {

        LOGGER.info("个人资料提交 请求参数：doctorParaRequestVO = [{}]", doctorParaRequestVO);

        // 校验参数
        doctorParaRequestVO.validator();

        this.doctorService.saveDoctorBase(doctorParaRequestVO);

        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);

    }

    /**
     * 修改医生签章图片
     * @param doctorId
     * @param sealImg
     * @return
     * @throws BusinessException
     */
    @ResponseBody
    @PostMapping("saveSealImg")
    @ApiOperation(value = "修改医生签章图片", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseVO<Void> saveSealImg(@RequestParam("doctorId") Long doctorId, @RequestParam("sealImg") MultipartFile sealImg) throws BusinessException {
        LOGGER.info("修改医生签章图片, doctorId {}, sealImg {}", doctorId, sealImg);
        this.doctorService.saveSealImg(doctorId, sealImg);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * <获取医生认证信息>
     * <详细描述>
     *
     * @param doctorId 医生id
     *
     * @return String <返回值描述>
     *
     * @Throws 异常信息
     * @History 2015年6月24日 下午7:29:12 by chenlin
     */
    @ResponseBody
    @RequestMapping("authData")
    @ApiOperation(value = "根据医生ID获取医生资质信息,工作证,职业资格证等信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "long", paramType = "query")})
    public ResponseVO<DoctorAuthVO> authData(Long doctorId) throws BusinessException {

        LOGGER.info("获取医生认证信息 请求参数：doctorId = [{}]", doctorId);

        DoctorAuthVO auth = this.doctorService.getDoctorAuthByDoctorId(doctorId);

        return PublicService.returnResponseVO(auth);

    }

    /**
     * （医生端优化1.1）获取医生端我的主页信息
     * 医生端1.1新需求
     * @param doctorId
     * @return
     * @throws BusinessException
     */
    @ResponseBody
    @PostMapping("mineInfo")
    @ApiOperation(value = "（医生端优化1.1）根据医生ID获取我的主页信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "long", paramType = "query")})
    public ResponseVO<DoctorMineVO> mineInfo(Long doctorId) throws BusinessException {
        LOGGER.info("获取医生认证信息 请求参数：doctorId = [{}]", doctorId);
        DoctorMineVO mine = this.doctorService.getDoctorMineInfo(doctorId);
        return PublicService.returnResponseVO(mine);
    }

    /**
     * （医生端优化1.1）我的资料编辑
     * @param doctorId
     * @param introduction
     * @param expertise
     * @return
     * @throws BusinessException
     */
    @ResponseBody
    @RequestMapping("updateMineInfo")
    @ApiOperation(value = "（医生端优化1.1）我的资料编辑", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "long", paramType = "form"),
            @ApiImplicitParam(name = "introduction", value = "简介", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "expertise", value = "擅长", required = true, dataType = "String", paramType = "form")
    })
    public ResponseVO<Void> updateMineInfo(Long doctorId, String introduction,String expertise) throws BusinessException {
        LOGGER.info("医生端优化1.1）我的资料编辑 请求参数：doctorId = {}, introduction = {}, expertise = {}", doctorId , introduction, expertise);
        doctorService.updateMineInfo(doctorId,introduction,expertise);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }


}
